package br.com.merks.service;

import java.math.BigDecimal;
import java.util.Map;

import br.com.caelum.vraptor.ioc.Component;
@Component
public class TradeAlgorithmImpl implements TradeAlgorithm {
	public static Double A = 100D; //mult
	public static Double B = 100D; //Control Depth Var to MSR
	/**
	 * Implementation of Market Score Rule developed by Robin Hanson
	 */
	public BigDecimal getCostPrice(Long answerOption, int tradeMerks,  Map<Long,Integer> answersMerksSum) {
		
		Double sumOfexp = 0D;
		for (Long key : answersMerksSum.keySet()) {
			if (!key.equals(answerOption)) {
				sumOfexp = sumOfexp + Math.exp( (answersMerksSum.get(key)/B ));
			}
		}
		
		int partMerks = 0;
		if (answersMerksSum.size()>0) partMerks = answersMerksSum.get(answerOption);
		Double costPrice = B * Math.log(Math.exp( (tradeMerks+partMerks) /B) + sumOfexp) - B * Math.log(Math.exp(partMerks/B) + sumOfexp);
		return new BigDecimal(A * costPrice);
	}
	public BigDecimal  getPrice(Long answerOption, int tradeMerks,  Map<Long,Integer> answersMerksSum) {
		Double sumOfexp = 0D;
		for (Long key : answersMerksSum.keySet()) {
			if (!key.equals(answerOption)) {
				sumOfexp = sumOfexp + Math.exp( (answersMerksSum.get(key)/B ));
			}
		}
		int partMerks = 0;
		if (answersMerksSum.size()>0) partMerks = answersMerksSum.get(answerOption);

		Double currentPrice = Math.exp( (tradeMerks+partMerks) /B)/(Math.exp((tradeMerks+partMerks)/B) + sumOfexp);
		return new BigDecimal(A * currentPrice);
	}
	
	/**
	 * Implementation of Pennock Dynamic Pari Mutuel Algorithm
	 * Double price = answerPrice * (Math.exp(2D * Math.sqrt(((double)partMerks + (double)tradeMerks) / (double)totalRestMerks) - 2D * Math.sqrt((double)partMerks / (double)totalRestMerks)) - 1.0D);
	 */
	
	
}
